﻿using System.Configuration;

namespace EasyLib.Configuration
{
    public sealed class FileConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("file", IsRequired = true, IsKey = true)]
        public string File
        {
            get
            {
                return this["file"] as string;
            }
            set
            {
                this["file"] = value;
            }
        }

        [ConfigurationProperty("category")]
        public string Category
        {
            get
            {
                return this["category"] as string;
            }
            set
            {
                this["category"] = value;
            }
        }
    }

    public sealed class FileConfigElementCollection : ConfigurationElementCollection
    {
        # region Overrided Methods&Properties

        protected override ConfigurationElement CreateNewElement()
        {
            return new FileConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as FileConfigElement).File;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        //Gets a value indicating whether an attempt to add a duplicate ConfigurationElement to the ConfigurationElementCollection 
        //will cause an exception to be thrown.
        protected override bool ThrowOnDuplicate
        {
            //Note that elements with identical keys and values are not considered duplicates, and are accepted silently. 
            //Only elements with identical keys but different values are considered duplicates.                       
            get
            {
                return true;
            }
        }

        #endregion

        #region Customize Indexer

        public FileConfigElement this[int index]
        {
            get
            {
                return BaseGet(index) as FileConfigElement;
            }
        }

        public new FileConfigElement this[string file]
        {
            get { return BaseGet(file) as FileConfigElement; }
        }

        #endregion
    }

    public sealed class FileConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("files")]
        public FileConfigElementCollection Files
        {
            get
            {
                return this["files"] as FileConfigElementCollection;
            }
            set
            {
                this["files"] = value;
            }
        }
    }
}